<?php

/*
	This file is part of Mandragon.

    Mandragon is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Mandragon is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Mandragon.  If not, see <http://www.gnu.org/licenses/>.
*/

class DbAccessor {
	
	function connect() {
		global $CONFIG;
		if (mysql_connect($CONFIG['db_host'], $CONFIG['db_user'], $CONFIG['db_pwd'])) {
			mysql_select_db($CONFIG['db_name']);
		} else {
			die("serious database error while connecting: " . mysql_error());
		}
	}

	function close() {
		mysql_close();
	}

	function db_do_query($query) {
		$result = mysql_query($query);
		if ($err = mysql_error()) {
			$alerter = new Alerter();
			$alerter->db_catch("<pre>ERROR: ".$err."\n\nON QUERY:\n\n$query</pre>", $err);
			return 0;
		}
		return $result;
	}

	function db_num_rows($result) {
		return intval(@mysql_num_rows($result));
	}

	function db_fetch_array($result) {
		return mysql_fetch_array($result, MYSQL_ASSOC);
	}

	function db_query_select($vars, $tables, $where, $order = "", $limit = "", $group = "", $having = "") {
		$query = "SELECT";
		while ($t = array_pop($tables)) {
			$va = array_pop($vars);
			if ($va) {
				$query .= "\n\t";
			}
			if (is_array($va)) {
				while ($v = array_pop($va)) {
					$query .= "$t.$v, ";
				}
			} else {
				$query .= "$va, ";
			}
			if ($t != "DUMMY") {
				$from .= "$t, ";
			}
		}
		$query .= "0\nFROM\n\t".substr($from, 0, -2);
		if ($where) {
			$query .= "\nWHERE\n\t$where";
		}
		if ($group) {
			$query .= "\nGROUP BY\n\t$group";
		}
		if ($having) {
			$query .= "\nHAVING\n\t$group";
		}
		if ($order) {
			$query .= "\nORDER BY\n\t$order";
		}
		if ($limit) {
			$query .= "\nLIMIT $limit";
		}
		return $query;
	}

	function db_query_insert($keys, $values, $table) {
		$query = "INSERT INTO $table";
		$k = array_pop($keys);
		$v = array_pop($values);
		while ($key = array_pop($keys)) {
			$k .= ", $key";
			$v .= ", ".array_pop($values);
		}
		$query .= "\n\t($k) VALUES\n\t($v)";
		return $query;
	}

	function db_query_insert_multiple($keys, $values, $table, $ignore=0) {
		$query = "INSERT " .($ignore ? "IGNORE " : ""). "INTO $table";
		$k = array_pop($keys);
		while ($key = array_pop($keys)) {
			$k .= ", $key";
		}
		while ($v = array_pop($values)) {
			$vs .= "(" . array_pop($v);
			while ($vv = array_pop($v)) {
				$vs .= ", $vv";
			}
			$vs .= "),";
		}
		$query .= "\n\t($k) VALUES\n\t".substr($vs, 0, -1);
		return $query;
	}

	function db_query_update($keys, $values, $table, $where) {
		$query = "UPDATE $table SET";
		$query .= "\n\t".array_pop($keys)." = ".array_pop($values);
		while ($key = array_pop($keys)) {
			$query .= ",\n\t$key = ".array_pop($values);
		}
		$query .= "\nWHERE\n\t$where";
		return $query;
	}

	function db_query_delete($table, $where) {
		return "DELETE FROM $table WHERE $where";
	}

	function db_insert_id() {
		return mysql_insert_id();
	}

	function db_affected_rows() {
		return mysql_affected_rows();
	}

	function db_sqlize($string) {
		return '"' . addslashes(trim($string)) . '"';
	}

	function db_desqlize($string) {
		return stripslashes($string);
	}

	function printquery($sql) {
		echo "<pre>$sql</pre>";
	}
}
?>
